<?php namespace Admin\Controller; 
use Hdphp\Controller\Controller;
use Common\Model\Goods;
use Common\Model\GoodsAttr;
use Common\Model\TypeAttr;

class GoodsController extends Controller{
	protected $goodsModel;
	
	public function __init(){
		$this->goodsModel = new \Common\Model\Goods;
	}	
	public function index(){
		$listsData = $this->goodsModel->get();
		
		View::with('listsData',$listsData);
		
		View::make();
	}
	
	/**
	 * 获得类型属性
	 */
	public function getAttr(){
		//得到类型id
		$tid = Q('post.t',0,'intval');
		//通过类型id找到类型属性
		$typeAttrModel = new \Common\Model\TypeAttr;
		$data = $typeAttrModel->where("type_tid={$tid}")->get();
		foreach ($data as $k => $v) {
			//把字符串变为数组，将来好循环
			$data[$k]['tavalue'] = explode(',', $v['tavalue']);
		}
		echo json_encode($data);exit;
	}
	
	public function edit(){
		$cateObj = new \Common\Model\Cate;
		$brandObj = new \Common\Model\Brand;
		 //1.获得旧数据
        $gid = Q('get.gid',0,'intval');
        $goodsModel = new Goods();
		if(IS_POST){
//			p(Q('post.'));exit;
			if($goodsModel->myedit()){
				View::success('编辑成功',U('Goods/index'));
			}
			View::error($goodsModel->getError());
		}
		 //1.获得旧数据
        $oldData = $goodsModel
                ->join('detail',C('database.prefix') . 'goods.gid','=',C('database.prefix') . 'detail.gid')
                ->where(C('database.prefix') . "goods.gid={$gid}")
                ->find();

		$oldData['small']=explode(',',$oldData['small']);
		array_pop($oldData['small']);
		
        View::with('oldData',$oldData);
		
		//获取商品分类
		$cates = $cateObj ->get();
		$cates_ = Data::tree($cates,'cname','cid','pid');
		View::with('cates',$cates_);
		
		//获取商品品牌
		$brands = $brandObj ->get();
		View::with('brands',$brands);
		
		//3.处理商品属性和规格
        $goodsAttrModel = new GoodsAttr();
        $data = $goodsAttrModel
                ->join('type_attr',C('database.prefix') . 'goods_attr.taid','=',C('database.prefix') . 'type_attr.taid')
                ->where("gid={$gid}")
                ->get();
        //获得taid就是为了以后排除已经选过的类型属性
        $taid = [];
        foreach ($data as $k => $v){
            $data[$k]['tavalue'] = explode(',',$v['tavalue']);
            $taid[] = $v['taid'];
        }
//        p($taid);
        //把商品没有选过属性也拿取出来
        $typeAttrModel = new TypeAttr();
        $taidStr = implode(',',$taid);
        $typeAttr = $typeAttrModel->where("taid NOT IN ({$taidStr}) AND type_tid={$oldData['type_tid']}")->get();

        foreach ($typeAttr as $v){
            $data[] = array(
                'gtvalue' => '',
                'taname'=>$v['taname'],
                'tavalue'=>explode(',',$v['tavalue']),
                'class'=>$v['class']
            );
        }
		View::with('data',$data);
		View::make();
	}
	
	public function add(){
		$cateObj = new \Common\Model\Cate;
		$brandObj = new \Common\Model\Brand;
		
		if(IS_POST){
//			p(Q('post.'));exit;
			if($this->goodsModel->myadd()){
				View::success('添加成功',U('Goods/index'));
			}
			View::error($this->goodsModel->getError());
		}
		//获取商品分类
		$cates = $cateObj ->get();
		$cates_ = Data::tree($cates,'cname','cid','pid');
		View::with('cates',$cates_);
		
		//获取商品品牌
		$brands = $brandObj ->get();
		View::with('brands',$brands);
		View::make();
	}
	
	
	  //uploadify异步上传
	public function upload()
	{
	    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
	    if (empty($file)) {
	        // 相当于：echo json_encode(Upload::getError());exit;
	        $this->ajax(Upload::getError());
	    } else {
	        
	        //上传成功，把上传好的信息返给js 也就是uploadify
	        $data = $file[0];
	        // 相当于：echo json_encode($data);exit;
	        $this->ajax($data);
	    }
	}
	
	public function uploadList()
	{
	    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
	    if (empty($file)) {
	        // 相当于：echo json_encode(Upload::getError());exit;
	        $this->ajax(Upload::getError());
	    } else {
	        //上传成功，把上传好的信息返给js 也就是uploadify
	        $data = $file[0];
	        // 相当于：echo json_encode($data);exit;
	        $this->ajax($data);
	    }
	}
	
	
	
}
